package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

//列表页
public class ListPage extends Utils {
    public static String url="http://8.148.81.42:8081/blog_list.html";


    public ListPage()
    {
        super(url);
    }


    /**
     1.检查博客列表---3个模块（个人信息模块，博客列表模块，菜单模块）
     */
    //博客列表模块
    public  void checkBlogList()
    {
        Utils.login();
        //博客标题--不能为空，否则异常,获取文本
        String title= driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
        //博客发布时间
        String pushTime=driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.date")).getText();
        //博客内容
        String content=driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.desc")).getText();
        //查找全文按钮
        String button=driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).getText();


        //校验文本
        assert !title.isEmpty();//不能为空
        assert !pushTime.isEmpty();
        assert !content.isEmpty();
        assert button.equals("查看全文>>");//文本保持一致

    }

    /**
     2.查看全文按钮的跳转
     */
    public void checkBtnJump() throws InterruptedException {

        //获取列表页博客标题
        String title= driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();

        //点击查看全文按钮实现跳转
        driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();

        //获取详情页博客标题
        Thread.sleep(3000);
        String jumpTitle=driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();


        //校验
        //跳转前后博客标题相同
        assert jumpTitle.equals(title);
        //编辑按钮
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button.edit-btn"));
        //删除按钮
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button.dele-btn"));

        //保存一份有效的URL
        detailUrl=driver.getCurrentUrl();



    }
    /**
     3.点击主页跳转到主页
     */
    public  void JumpList(){

        //点击主页按钮
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
        //跳转到主页----主页元素
        assert driver.getCurrentUrl().equals("http://8.148.81.42:8081/blog_list.html");
    }

    /**
     4.点击写博客按钮跳转到博客编辑页
     */
    public  void  JumpEdit(){
        //点击写博客按钮
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        //跳转到博客编辑页
        assert driver.getCurrentUrl().equals("http://8.148.81.42:8081/blog_edit.html");

        //返回上一级-----列表页
        driver.navigate().back();
    }

    /**
     5.点击注销按钮跳转到用户登录
     */
    public  void JunpLogout() throws InterruptedException {
        //点击注销按钮
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        //等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());

        //处理错误弹窗

        //选择“取消”---返回当前页
        Alert cancelAlert = driver.switchTo().alert();
        cancelAlert.dismiss(); // 点击取消
       // Thread.sleep(2000);
        // 验证页面不变
        assert driver.getCurrentUrl().equals("http://8.148.81.42:8081/blog_list.html#");


        //点击注销按钮
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        //等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());
        //选择”确定”----跳转到用户登录
        Alert alert = driver.switchTo().alert();
        alert.accept();


    }


}
